package view.components
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	
	import global.Resource;
	
	import utils.ColorTool;
	import utils.EnterFrameTimer;
		
	public class HintImgButton extends Button
	{
		private var bg:DisplayObject;
		private var face:DisplayObject;
		private var timer:EnterFrameTimer;
		private var switchHint:Boolean;
		private var _isHint:Boolean
		
		public function HintImgButton(x:int, y:int, id:int, onClickFunction:Function,bg:DisplayObject,
									  width:int,height:int,faceKey:String,faceX:int= -1,faceY:int = -1)
		{
			super(x, y, id, onClickFunction);
			this.mouseChildren = false;
			this.buttonMode = true;
			this.bg = bg;
			if (width != -1)
			{
				bg.width = width;
				bg.height = height;
				
			}
			addChild(bg);
			
			if (faceKey)
			{
				face = Resource.getImg(faceKey);
				if (faceX != -1)
				{
					face.x = faceX;
					face.y = faceY;
				}
				else
				{	
					face.x = (width - face.width) * .5;
					face.y = (height - face.height) * .5;
				}
				addChild(face);
			}
			initTimer();
		}
		
		public function get isHint():Boolean
		{
			return _isHint;
		}

		public function set isHint(v:Boolean):void
		{
			_isHint = v;
		}

		private function initTimer():void
		{
			timer = new EnterFrameTimer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			if (!switchHint)
			{
				ColorTool.red(bg);
				switchHint = true;
			}
			else
			{
				ColorTool.saturation(bg);
				switchHint = false;
			}
		}
		
		public function startHint():void
		{
			timer.start();
			_isHint = true;
		}
		
		public function stopHint():void
		{
			timer.stop();
			ColorTool.saturation(bg);
			switchHint = false;
			_isHint = false;
		}
		
		override protected function onMouseOver(event:MouseEvent):void
		{
			
			trace("hit");
			if (_info)
			{
				_control.hintControl.showHint(this,"<font color='#663300' size='12'>" + _info + "</font>",_isUpInfo);
			}
			if (_isHint)
			{
				timer.stop();
			}
			this.buttonMode = true;	
			ColorTool.red(bg);
			
			
			
		}
		
		override protected function onMouseOut(event:MouseEvent):void
		{
			if (_info)
			{
				_control.hintControl.hideHint();
			}
			ColorTool.saturation(bg);
			
			if (_isHint)
			{
				timer.start();
			}
		}
	}
}